﻿using MSL.Tool;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using TimedTask.Models;
using TimedTask.Tools;

namespace TimedTask.Core
{
    public class WeatherHelper
    {
        private static Tools.Calendar calender = new Tools.Calendar(DateTime.Now);
        /// <summary>
        /// 获取天气信息
        /// </summary>
        /// <param name="cityName"></param>
        /// <returns></returns>
        public static WeatherInfo GetWeatherInfo(String cityName, string cityCode)
        {
            WeatherInfo weather = new WeatherInfo();
            if (String.IsNullOrEmpty(cityCode))
            {
                return weather;
            }
            try
            {
                string url = Const.WeatherUrl + System.Web.HttpUtility.UrlEncode(cityCode);// "http://wthrcdn.etouch.cn/weather_mini?citykey=101010100";// String.Format(Const.WeatherUrl, cityCode);
                string html = HtmlHelper.GetHtml(ref url, "utf-8");

                Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(html);
                if (!dic["status"].ToString().Equals("200"))
                {
                    return weather;
                }
                string json = dic["data"].ToString();

#if DEBUG
                Console.WriteLine("获取天气信息为: " + html);
#endif

                weather = JsonConvert.DeserializeObject<WeatherInfo>(json);
                weather.City = cityName;
                weather.CurrWenDu = weather.CurrWenDu + "℃";
                weather.Date = DateTime.Now.ToString("yyyy年MM月dd日 农历 " + calender.ChineseDateString.Substring(calender.ChineseDateString.IndexOf("年") + 1));
                weather.Date += " (" + DateTime.Now.ToString("HH:mm") + "更新)";

                List<WeatherDay> wDays = new List<WeatherDay>();
                int i = 0;
                foreach (var wDay in weather.WeatherDays)
                {
                    if (i == 0)
                    {
                        weather.Type = wDay.Type;
                        weather.Image = GetImgUrl(weather.Type).Replace("2.png", ".png");
                        weather.Temperature = wDay.Low.Replace("低温 ", "").Replace("℃", "") + " ~ " + wDay.High.Replace("高温 ", "");

                        wDay.Date = "今天(周" + wDay.Date.Substring(wDay.Date.Length - 1) + ")";
                    }
                    else if (i == 1)
                    {
                        wDay.Date = "明天(周" + wDay.Date.Substring(wDay.Date.Length - 1) + ")";
                    }
                    else if (i == 2)
                    {
                        wDay.Date = "后天(周" + wDay.Date.Substring(wDay.Date.Length - 1) + ")";
                    }
                    else
                    {
                        wDay.Date = "周" + wDay.Date.Substring(wDay.Date.Length - 1);
                    }
                    wDay.Temperature = wDay.Low.Replace("低温 ", "").Replace("℃", "") + " ~ " + wDay.High.Replace("高温 ", "");
                    wDay.Image = GetImgUrl(wDay.Type);
                    wDay.WindInfo = wDay.Fengli + " " + wDay.Fengxiang;

                    wDays.Add(wDay);
                    i++;
                }
                weather.WeatherDays = wDays;
            }
            catch (Exception ex)
            {
                LogHelper.SaveErrorLog("获取天气信息异常", ex);
            }
            return weather;
        }

        /// <summary>
        /// 取本地图片路径
        /// </summary>
        /// <param name="imgName"></param>
        /// <returns></returns>
        private static string GetImgUrl(string imgName)
        {
            string src = "";
            string src1 = "";

            switch (imgName)
            {
                case "多云":
                    src = "http://open.sojson.com/images/tianqi/cloud.png";
                    src1 = "http://open.sojson.com/images/tianqi/cloud2.png";
                    break;
                case "晴":
                    src = "http://open.sojson.com/images/tianqi/fine.png";
                    src1 = "http://open.sojson.com/images/tianqi/fine2.png";
                    break;
                case "阴":
                    src = "http://open.sojson.com/images/tianqi/overcast.png";
                    src1 = "http://open.sojson.com/images/tianqi/overcast2.png";
                    break;
                case "小雨":
                    src = "http://open.sojson.com/images/tianqi/small_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/small_rain2.png";
                    break;
                case "小到中雨":
                    src = "http://open.sojson.com/images/tianqi/stom_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/stom_rain2.png";
                    break;
                case "大雨":
                case "中到大雨":
                    src = "http://open.sojson.com/images/tianqi/big_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/big_rain2.png";
                    break;
                case "暴雨":
                case "大暴雨":
                case "特大暴雨":
                case "大到暴雨":
                case "暴雨到大暴雨":
                case "大暴雨到特大暴雨":
                    src = "http://open.sojson.com/images/tianqi/mbig_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/mbig_rain2.png";
                    break;
                case "雨夹雪":
                    src = "http://open.sojson.com/images/tianqi/rain_snow.png";
                    src1 = "http://open.sojson.com/images/tianqi/rain_snow2.png";
                    break;
                case "阵雪":
                    src = "http://open.sojson.com/images/tianqi/quick_snow.png";
                    src1 = "http://open.sojson.com/images/tianqi/quick_snow2.png";
                    break;
                case "雾":
                    src = "http://open.sojson.com/images/tianqi/fog.png";
                    src1 = "http://open.sojson.com/images/tianqi/fog2.png";
                    break;
                case "沙尘暴":
                case "浮尘":
                case "扬沙":
                case "强沙尘暴":
                case "雾霾":
                    src = "http://open.sojson.com/images/tianqi/sand.png";
                    src1 = "http://open.sojson.com/images/tianqi/sand2.png";
                    break;
                case "冻雨":
                    src = "http://open.sojson.com/images/tianqi/ice_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/ice_rain2.png";
                    break;
                case "中雨":
                    src = "http://open.sojson.com/images/tianqi/mid_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/mid_rain2.png";
                    break;
                case "雷阵雨伴有冰雹":
                    src = "http://open.sojson.com/images/tianqi/quick_rain_ice.png";
                    src = "http://open.sojson.com/images/tianqi/quick_rain_ice2.png";
                    break;
                case "阵雨":
                    src = "http://open.sojson.com/images/tianqi/quick_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/quick_rain2.png";
                    break;
                case "雷阵雨":
                    src = "http://open.sojson.com/images/tianqi/lquick_rain.png";
                    src1 = "http://open.sojson.com/images/tianqi/lquick_rain2.png";
                    break;
                case "无天气类型":
                    src = "http://open.sojson.com/images/tianqi/unknown.png";
                    src1 = "http://open.sojson.com/images/tianqi/unknown2.png";
                    break;
                default:
                    src = "http://open.sojson.com/images/tianqi/unknown.png";
                    src1 = "http://open.sojson.com/images/tianqi/unknown2.png";
                    break;
            }
            string path = Const.StartPath + "\\Weather\\" + src1.Substring(src1.LastIndexOf('/') + 1);
            if (!File.Exists(path))
            {
                Helper.DownloadFileByWebRequest(src, path);
                Helper.DownloadFileByWebRequest(src.Replace(".png", "2.png"), path.Replace(".png", "2.png"));
            }
            return path;
        }
    }
}
